package com.hertz.iris.util;

import java.io.IOException;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Vibrator;

public class MediaPlayerHelper {
	private MediaPlayer mMediaPlayer;
	private Context context;
	private Vibrator vibrator;
	
	public MediaPlayerHelper(Context context){
		this.context = context;
		vibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);  
	}
	
	public void playRingtone(String uri,float volume,boolean isWave) {
	    stopRingtone();
	    mMediaPlayer = new MediaPlayer();
		try {
			if (!Strings.isBlank(uri)) {
				mMediaPlayer.setDataSource(uri);
				mMediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
				mMediaPlayer.setVolume(volume, volume);
				mMediaPlayer.setLooping(true);
				mMediaPlayer.prepare();
				mMediaPlayer.start();
			}

			if (isWave)
				vibrator.vibrate(new long[] { 0, 200, 500 }, 0);
		} catch (IOException e) {
	      Ln.w("TonePicker", "Unable to play track: "+uri != null ? uri.toString() : "null");
	      e.printStackTrace();
	    }
	  }
	
	public void stopRingtone() {
	    if (mMediaPlayer != null) {
	      mMediaPlayer.stop();
	      mMediaPlayer.release();
	      mMediaPlayer = null;
	    }
	    vibrator.cancel();	
	  }
	
}
